- /* slcomplx.h by K.Tsuru */
- /*********************************************************
- SN library
- SLComplex class
- It provides a multi-precision complex number arithmetic.
- SLong version is applied to binary splitting method.
- *********************************************************/
- #ifndef SLCOMPLEX_H
- #define SLCOMPLEX_H
-
- class SLComplex{
- private:
- SLong re, im;
- /********* List of unusable functions which have no body.******/
- SLComplex operator>(const SLComplex&) const;
- SLComplex operator>=(const SLComplex&) const;
- SLComplex operator<(const SLComplex&) const;
- SLComplex operator<=(const SLComplex&) const;
- // division operators are not provided.
- SLComplex& operator/=(const SLong& x);
- SLComplex& operator/=(const SLComplex& z);
- public:
- // default constructor do nothing.
- SLComplex() {}
- // constructor by two SLong values
- SLComplex(const SLong& rp, const SLong& ip = 0.0) : re(rp), im(ip){}
- // constructor by two double values since ver 2.18
- // This enables us to use "return 0.0".
- // SLComplex(double rp, double ip = 0.0) : re(rp), im(ip){}
- // copy constructor
- SLComplex(const SLComplex& z) :re(z.re), im(z.im){}
- ~SLComplex(){}
-
- SLong Real() const { return re;} // real part
- SLong Imag() const { return im;} // imaginary part
- SLong Norm() const;
- SLComplex Conj() const;
-
- uint Head() const { return min( re.Head(), im.Head() ); }
- uint Tail() const { return min( re.Tail(), im.Tail() ); }
- void FigureClear(uint from, uint to) {
- re.FigureClear(from, to); im.FigureClear(from, to);
- }
- void SetInt(int i) { re.SetInt(i); im.SetZero(); }
- void SetZero(){ re.SetZero(); im.SetZero(); }
- // If you want to change the real part only, use such as z.Set(newValue, z.Imag());
- // The statement im = z.Imag(); does not copy, because &im == &ip.
- void Set(const SLong& rp, const SLong& ip) { re = rp; im = ip; }
-
- bool IsZero(int id=91) const { return (re.Sign(id) == 0) && (im.Sign(id) == 0); }
- //sign operators
- SLComplex operator+() const { return *this; }
- SLComplex operator-() const;
-
- //Operators whose rhs is scalar.
- SLComplex& operator=(const SLong& x);
- SLComplex& operator+=(const SLong& x);
- SLComplex& operator-=(const SLong& x);
- SLComplex& operator*=(const SLong& x);
-
- //Operators whose rhs is complex object.
- SLComplex& operator=(const SLComplex& z);
- SLComplex& operator+=(const SLComplex& z);
- SLComplex& operator-=(const SLComplex& z);
- SLComplex& operator*=(const SLComplex& z); // 902
-
- // At the present time it does not support reading from file.
- // Please use SLong class' Put(s) and write/read the real and imaginary parts, separately.
- // Due to the value of "fmt" it outputs in the following form.
- // iFMT : (real part)[+|-]i*(imaginary part)
- // BracketFMT : (real part, imaginary part)
- enum { iFMT = 0, BracketFMT};
- long Put(bool crlf = false, int fmt = iFMT) const; // 901
- long Puts(int fmt = iFMT) const{ return Put(true, fmt); }
- };
-
- ///// Cautions : All functions having its body must be "inline".///////
- /*
- inline long SLComplex::Put(bool crlf, int fmt) const {
- SComplex r(re, im);
- return r.Put(crlf, fmt);
- }
- */
- /**********************************
- * Implementation of member functons
- ***********************************/
- inline SLong SLComplex::Norm() const {
- return ( re * re + im * im );
- }
-
- inline SLComplex SLComplex::Conj() const { return SLComplex(re, -im); }
-
- inline SLComplex SLComplex::operator-() const {
- SLComplex z(*this);
- z.re = -z.re; z.im = -z.im;
- return z;
- }
-
- ////// Operators whose rhs is real scalar. //////
- inline SLComplex& SLComplex::operator=(const SLong& x) {
- re = x; im = 0.0; return *this;
- }
-
- inline SLComplex& SLComplex::operator+=(const SLong& x) {
- re += x; return *this;
- }
-
- inline SLComplex& SLComplex::operator-=(const SLong& x) {
- re -= x; return *this;
- }
-
- inline SLComplex& SLComplex::operator*=(const SLong& x) {
- re *= x; im *= x; return *this;
- }
-
- ///// Operators whose rhs is complex object. /////
- inline SLComplex& SLComplex::operator=(const SLComplex& z) {
- if(this == &z) return *this; // z = z;
- re = z.re; im = z.im;
- return *this;
- }
-
- inline SLComplex& SLComplex::operator+=(const SLComplex& z) {
- re += z.re; im += z.im;
- return *this;
- }
-
- inline SLComplex& SLComplex::operator-=(const SLComplex& z) {
- re -= z.re; im -= z.im;
- return *this;
- }
-
- /******* Non menber functions in the following *******/
- //// Two term operators /////
- //// plus operators x + y
- inline SLComplex operator+(const SLComplex& x, const SLComplex& y) {
- return SLComplex(x.Real() + y.Real(), x.Imag() + y.Imag());
- }
-
- inline SLComplex operator+(const SLComplex& x, const SLong& y) {
- SLComplex r = x;
- return r += y;
- }
-
- inline SLComplex operator+(const SLong& x, const SLComplex& y) {
- SLComplex r = y;
- return r += x;
- }
-
- //// minus operators x - y
- inline SLComplex operator-(const SLComplex& x, const SLComplex& y) {
- return SLComplex(x.Real() - y.Real(), x.Imag() - y.Imag());
- }
- inline SLComplex operator-(const SLComplex& x, const SLong& y) {
- SLComplex r = x;
- return r -= y;
- }
- inline SLComplex operator-(const SLong& x, const SLComplex& y) {
- SLComplex r = x;
- return r -= y;
- }
- //// multiplication operators x * y
- inline SLComplex operator*(const SLComplex& x, const SLComplex& y) {
- SLComplex r = x;
- return r *= y;
- }
-
- inline SLComplex operator*(const SLComplex& x, const SLong& y) {
- SLComplex r = x;
- return r *= y;
- }
- inline SLComplex operator*(const SLong& x, const SLComplex& y) {
- SLComplex r = y;
- return r *= x;
- }
-
- /*****************
- basic functions
- ******************/
- inline SLComplex Conj(const SLComplex& z) { // conjugate
- return z.Conj();
- }
- /*
- SLong Cabs(const SLComplex& z); // |z|
- SLong Arg(const SLComplex& z); // argument in xy plane
- */
- inline SLong Real(const SLComplex& z){ return z.Real();} // real part
- inline SLong Imag(const SLComplex& z){ return z.Imag();} // imaginary part
- inline SLong Norm(const SLComplex& z){ return z.Norm(); } // square of the magnitude
- //// relational operators
- inline bool operator==(const SLComplex& x, const SLComplex& y){
- return (x.Real()==y.Real()) && (x.Imag()==y.Imag());
- }
- inline bool operator!=(const SLComplex& x, const SLComplex& y){
- return !(x == y);
- }
-
- inline SComplex SLCtoSC(const SLComplex& x) {
- return SComplex(x.Real(), x.Imag());
- }
- #endif // SLCOMPLEX_H
slcomplx.h : last modifiled at 2016/09/04 14:21:36(6,413 bytes)
created at 2016/04/11 11:18:59
The creation time of this html file is 2017/10/11 16:07:52 (Wed Oct 11 16:07:52 2017).